package de.baumann.browser.activitys.user

import android.app.Activity
import android.text.InputFilter
import android.text.InputType
import de.baumann.browser.R
import de.baumann.browser.utils.Constants
import de.baumann.browser.utils.ViewUtil
import kotlinx.android.synthetic.main.activity_edit_userinfo.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 * @author：
 * @date：2018/10/22
 * @discription:
 */
class EditUserInfoActivity : BaseEditUserInfoActivity() {

    private var editType: Int = 0

    override fun getLayout(): Int {
        return R.layout.activity_edit_userinfo
    }

    override fun initView() {
        euRoot.setPadding(0, ViewUtil.getStatusBarHeight(mContext), 0, 0)
        initToolbar(commToolBar, commTitle)
        btnComplete.setOnClickListener {
            when (editType) {
                Constants.EDIT_TYPE_NICKNAME -> presenter.editNickname()
                Constants.EDIT_TYPE_QQ -> presenter.editQQ()
            }
        }
    }

    override fun initData() {
        val intent = intent
        editType = intent.getIntExtra(Constants.EDIT_TYPE, Constants.EDIT_TYPE_NICKNAME)
        when (editType) {
            Constants.EDIT_TYPE_NICKNAME -> {
                commTitle.text = getString(R.string.set_nickname)
                edContent.inputType = InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE
                val nickname = getIntent().getStringExtra(Constants.NICK_NAME)
                edContent.setText(nickname)
                edContent.setHint(R.string.nickname_empty)
                edContent.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(10))
            }
            Constants.EDIT_TYPE_QQ -> {
                commTitle.text = getString(R.string.qq_number)
                edContent!!.inputType = InputType.TYPE_CLASS_NUMBER
                val qq = getIntent().getStringExtra(Constants.QQ_CODE)
                edContent!!.setText(qq)
                edContent!!.setHint(R.string.qq_empty)
                edContent!!.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
            }
        }
    }

    override fun getContent(): String {
        return edContent.text.trim().toString()
    }

    override fun editSuccess() {
        when (editType) {
            Constants.EDIT_TYPE_NICKNAME -> setResult(Activity.RESULT_OK, intent.putExtra(Constants.NICK_NAME, content))
            Constants.EDIT_TYPE_QQ -> setResult(Activity.RESULT_OK, intent.putExtra(Constants.QQ_CODE, content))
        }
        finish()
    }
}
